package com.pyg.goods.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;

import com.pyg.pojo.TbAddressExample;
import com.pyg.pojo.TbAddressExample.Criteria;
import com.pyg.mapper.TbAddressMapper;
import com.pyg.pojo.TbAddress;
import com.pyg.goods.service.TbAddressService;

import com.pyg.utils.PygStringUtils;
import entity.PageResult;

/**	*  @Author zhuliangcai
*  @Email zhuliangcai@itcast.cn
*  @Date 2018-04-03 20:07:45
*/
@Service
public class TbAddressServiceImpl implements TbAddressService {
	@Autowired
	private TbAddressMapper tbAddressMapper;

	@Override
	public List<TbAddress> findAll() {
		return tbAddressMapper.selectByExample(null);
	}

	@Override
	public PageResult findPage(int pageNum, int pageSize) {
		// 开启分页插件功能
		PageHelper.startPage(pageNum, pageSize);

		Page<TbAddress> page = (Page<TbAddress>) tbAddressMapper.selectByExample(null);

		return new PageResult(page.getTotal(), page.getResult());
	}

	/**
	 * 品牌条件分页
	 *
	 * @param TbAddress
	 *            查询条件
	 * @param pageNum
	 *            当前页面
	 * @param pageSize
	 *            每页记录数
	 * @return
	 */
	@Override
	public PageResult findPage(TbAddress tbAddress, int pageNum, int pageSize) {
		// 开启分页插件功能
		PageHelper.startPage(pageNum, pageSize);

		TbAddressExample example = null;
		if (tbAddress != null) {
			example = new TbAddressExample();
			Criteria criteria = example.createCriteria();
			if (PygStringUtils.isNotEmpty(tbAddress.getUserId())) {
				criteria.andUserIdLike("%" + tbAddress.getUserId() + "%");
			}
			if (PygStringUtils.isNotEmpty(tbAddress.getProvinceId())) {
				criteria.andProvinceIdLike("%" + tbAddress.getProvinceId() + "%");
			}
			if (PygStringUtils.isNotEmpty(tbAddress.getCityId())) {
				criteria.andCityIdLike("%" + tbAddress.getCityId() + "%");
			}
			if (PygStringUtils.isNotEmpty(tbAddress.getTownId())) {
				criteria.andTownIdLike("%" + tbAddress.getTownId() + "%");
			}
			if (PygStringUtils.isNotEmpty(tbAddress.getMobile())) {
				criteria.andMobileLike("%" + tbAddress.getMobile() + "%");
			}
			if (PygStringUtils.isNotEmpty(tbAddress.getAddress())) {
				criteria.andAddressLike("%" + tbAddress.getAddress() + "%");
			}
			if (PygStringUtils.isNotEmpty(tbAddress.getContact())) {
				criteria.andContactLike("%" + tbAddress.getContact() + "%");
			}
			if (PygStringUtils.isNotEmpty(tbAddress.getIsDefault())) {
				criteria.andIsDefaultLike("%" + tbAddress.getIsDefault() + "%");
			}
			if (PygStringUtils.isNotEmpty(tbAddress.getNotes())) {
				criteria.andNotesLike("%" + tbAddress.getNotes() + "%");
			}
			if (PygStringUtils.isNotEmpty(tbAddress.getCreateDate())) {
				criteria.andCreateDateEqualTo(tbAddress.getCreateDate() );
			}
			if (PygStringUtils.isNotEmpty(tbAddress.getAlias())) {
				criteria.andAliasLike("%" + tbAddress.getAlias() + "%");
			}
		}

		Page<TbAddress> page = (Page<TbAddress>) tbAddressMapper.selectByExample(example);

		return new PageResult(page.getTotal(), page.getResult());
	}

	@Override
	public void add(TbAddress tbAddress) {
		tbAddressMapper.insert(tbAddress);
	}

	@Override
	public void del(Long[] ids) {
		for (Long id : ids) {
			tbAddressMapper.deleteByPrimaryKey(id);
		}
	}

	@Override
	public TbAddress findOne(Long id) {
		return tbAddressMapper.selectByPrimaryKey(id);
	}

	@Override
	public void update(TbAddress tbAddress) {
		tbAddressMapper.updateByPrimaryKey(tbAddress);
	}

}
